using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Storage
{
	public class ChatMsgStorage
	{
		private const int chatmsg_id_base = 1;
		private const int qmsg_id_base = 2000000;
		private const int bmsg_id_base = 4000000;
		private const int tmsg_id_base = 6000000;
		private const string TAG_QQ = "@qqim";
		private const string TAG_BOTTLE = "@bottle";
		private ChatMsgTable chatMsg = new ChatMsgTable(null, 1);
		private ChatMsgTable qMsg = new ChatMsgTable("QChatMsg", 2000000);
		private ChatMsgTable bMsg = new ChatMsgTable("BChatMsg", 4000000);
		private ChatMsgTable[] msgTables;
		public ChatMsgStorage()
		{
			this.msgTables = new ChatMsgTable[]
			{
				this.chatMsg,
				this.qMsg,
				this.bMsg
			};
		}
		private ChatMsgTable getTable(string talker)
		{
			if (string.IsNullOrEmpty(talker))
			{
				DebugEx.debugBreak();
				return this.chatMsg;
			}
			if (talker.EndsWith("@qqim"))
			{
				return this.qMsg;
			}
			if (talker.Contains("@bottle"))
			{
				return this.bMsg;
			}
			return this.chatMsg;
		}
		private ChatMsgTable getTable(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 != null && !string.IsNullOrEmpty(chatMsg_0.strTalker))
			{
				return this.getTable(chatMsg_0.strTalker);
			}
			return this.chatMsg;
		}
		private ChatMsgTable getTable(int nMsgLocalID)
		{
			if (nMsgLocalID < 2000000)
			{
				return this.chatMsg;
			}
			if (nMsgLocalID < 4000000)
			{
				return this.qMsg;
			}
			if (nMsgLocalID < 6000000)
			{
				return this.bMsg;
			}
			return this.chatMsg;
		}
		public ChatMsg getMsg(int nMsgLocalID)
		{
			return this.getTable(nMsgLocalID).getMsg(nMsgLocalID);
		}
		public ChatMsg getBySvrID(string strTalker, int nMsgSvrID)
		{
			if (string.IsNullOrEmpty(strTalker))
			{
				DebugEx.debugBreak();
			}
			return this.getTable(strTalker).getBySvrID(nMsgSvrID);
		}
		public bool addMsg(ChatMsg chatMsg_0)
		{
			return this.getTable(chatMsg_0).addMsg(chatMsg_0);
		}
		public bool delMsg(ChatMsg chatMsg_0)
		{
			return this.getTable(chatMsg_0).delMsg(chatMsg_0);
		}
		public bool delMsg(string strTalker, int nMsgSvrID)
		{
			return this.getTable(strTalker).delMsgBySvrID(nMsgSvrID);
		}
		public void delByTalker(string talker)
		{
			this.getTable(talker).delByTalker(talker);
		}
		public bool modifyMsg(ChatMsg item)
		{
			return this.getTable(item).modifyMsg(item);
		}
		public bool updateMsg(ChatMsg item)
		{
			return this.getTable(item).updateMsg(item);
		}
		public int getMsgNum(string talker)
		{
			return this.getTable(talker).getMsgNum(talker);
		}
		public bool updateByMsgSvrID(ChatMsg item)
		{
			return this.getTable(item.strTalker).updateBySvrID(item);
		}
		public List<ChatMsg> getMsgList(string talker, int start, int int_0)
		{
			List<ChatMsg> result;
			try
			{
				result = this.getTable(talker).getMsgList(talker, start, int_0);
			}
			catch (Exception)
			{
				result = new List<ChatMsg>();
			}
			return result;
		}
		public List<ChatMsg> searhCacheMsg(int nMsgType, int nMsgStatus, int isSender = 1)
		{
			return this.chatMsg.searhCacheMsg(nMsgType, nMsgStatus, isSender);
		}
		public bool updateMsgList(List<ChatMsg> msgList)
		{
			if (msgList != null && msgList.get_Count() > 0)
			{
				using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						this.getTable(current.strTalker).updateMsg(current);
					}
				}
				return true;
			}
			return false;
		}
		public bool updateListByMsgSvrID(List<ChatMsg> msgList)
		{
			if (msgList != null && msgList.get_Count() > 0)
			{
				using (List<ChatMsg>.Enumerator enumerator = msgList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						this.getTable(current.strTalker).updateBySvrID(current);
					}
				}
				return true;
			}
			return false;
		}
		public void clearAllMsg()
		{
			ChatMsgTable[] array = this.msgTables;
			for (int i = 0; i < array.Length; i++)
			{
				ChatMsgTable chatMsgTable = array[i];
				chatMsgTable.clearAllMsg();
			}
		}
		public void patch_mov_qq_msg()
		{
			if (!StorageIO.dirExists(StorageIO.getHeadImgQQPath()))
			{
				return;
			}
			List<ChatMsg> list = this.chatMsg.patch_get_qmsg();
			if (list != null && Enumerable.Count<ChatMsg>(list) > 0)
			{
				using (List<ChatMsg>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						ChatMsg chatMsg = new ChatMsg();
						chatMsg.nMsgLocalID = current.nMsgLocalID;
						chatMsg.nMsgSvrID = current.nMsgSvrID;
						chatMsg.nMsgType = current.nMsgType;
						chatMsg.nStatus = current.nStatus;
						chatMsg.nIsSender = current.nIsSender;
						chatMsg.nCreateTime = current.nCreateTime;
						chatMsg.strTalker = current.strTalker;
						chatMsg.strContent = current.strContent;
						chatMsg.strClientMsgId = current.strClientMsgId;
						chatMsg.bytesXmlData = Util.byteArrayClone(current.bytesXmlData);
						this.qMsg.addMsg(chatMsg);
					}
				}
				return;
			}
		}
		public void dumpTable()
		{
			this.bMsg.dumpTable();
		}
		public void test()
		{
			StorageBasic.onLogin("test");
		}
	}
}
